﻿<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:replace="layout :: common_header(~{::title})">
    <title>仓位管理-仓库-wms仓库管理系统</title>
</head>
<body th:replace="layout :: common_body(~{::div.layui-body})">

<div class="layui-body" style="padding:15px;top: 0px;">

    <!-- start sp-wrapper -->
    <link rel="stylesheet" href="/sb/stock.css"/>
    <div class="layui-tab">
        <ul class="layui-tab-title">
            <li ><a href="/stock_location/list">仓库管理</a></li>
            <li  ><a href="/stock_location/reservoir">库区管理</a></li>
            <li class="layui-this"><a href="/stock_location/shelf">仓位管理</a></li>
        </ul>
    </div>
    <div class="sp-wrapper">
<!--        <h2 class="sp-title"><span>仓位管理</span></h2>-->
        <div class="sp-tool">

            <form action="/stock_location/shelf" method="get">
                <span class="fl">
                    <b><input type="text" name="number" id="number" th:value="${number}" placeholder="仓位编码" style="width: 200px;"></b>
                    <input type="submit" class="button" value="查询">
                    <b>总共：&nbsp;[[${totalSize}]]&nbsp;条记录</b>
                </span>
            </form>
        </div>
        <div class="layui-form-item">

            <div class="layui-inline">
                <form method="post" enctype="multipart/form-data" id="execl_form">
                    <input type="file" name="excel" id="excel" placeholder="excel" class="layui-input">
                </form>
            </div>

            <div class="layui-inline">
                <button type="button" id="excel_upload_btn" class="layui-btn">批量导入仓位信息</button>
                <a href="/importTemplates/store_house_info_import_template.xls" target="_blank">下载仓位导入模版</a>
            </div>
            <div class="layui-inline">
                <button type="button" id="add" class="layui-btn add">新增仓位</button>
<!--            <span class="fr"><i class="add" id="add">新增</i></span>-->
            </div>
        </div>
        <div class="sp-table">
            <table>
                <thead>
                <tr>
                    <th>仓位编号</th>
                    <th>仓位名称</th>
                    <th>所属仓库</th>
                    <th>所属库区</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <tr th:each="item:${shelf}">
                    <td th:text="${item.getNumber()}"></td>
                    <td th:text="${item.getName()}"></td>
                    <td th:text="${item.name1}"></td>
                    <td th:text="${item.name2}"></td>
                    <td>
                        <i th:attr="data-id=${item.getId()},data-number=${item.getNumber()},data-name=${item.getName()},data-store_house=${item.parentId1},data-reservoir=${item.parentId2}"
                           class="edit">修改</i>
                        <i th:attr="data-id=${item.getId()}" class="delete">删除</i>
                    </td>
                </tr>
                </tbody>
            </table>
            <div class="layui-box layui-laypage layui-laypage-default pageBox">
                <paging:pager th:value="${pageIndex}" th:rows="${totalSize}" th:size="${pageSize}"/>
            </div>
        </div>
    </div>

    <!-- 模板 新增/修改 -->
    <script type="text/template" id="tpl-1">
        <div class="T-box">
            <p><span>所属仓库</span>
                <select id="fl-store_house" name="houseId" onchange="addVideo(this.value)">
                    <option selected value="">请选择</option>
                    <option th:each="StoreHouseEntity:${house}" th:value="${StoreHouseEntity.getId()}"
                            th:text="${StoreHouseEntity.getName()}"></option>
                </select>
            </p>
            <p><span>所属库区</span>
                <select id="fl-reservoir">
                    <option selected value="">请选择</option>
<!--                      <option th:each="item:${reservoir}"  th:value="${item.getId()}" th:text="${item.getName()}" ></option>-->
                </select>
            </p>
            <p><span>仓位编号</span><input id="fl-number" type="text"/></p>
<!--            <p><span>仓位名称</span><input id="fl-name" type="text"/></p>-->
        </div>
    </script>
    <script type="text/javascript" src="/sb/jquery.min.js"></script>
    <script type="text/javascript" src="/sb/Tips.min.js"></script>
    <script type="text/javascript">
        function addVideo(id) {
            // var id = $("#fl-store_house").val();
            if(id==''|| id==0 ){
                var str = "<option value = ''>请选择</option>";
                $("#fl-reservoir").html(str);
            }else {
                $.ajax({
                    type: "POST",
                    url: "/ajax_stock/get_reservoir",
                    data: id,
                    dataType: 'json',
                    contentType: 'application/json',
                    success: function (data) {
                        var str = "<option value = ''>请选择</option>";
                        var data1 = data.data;
                        for (var i = 0; i < data1.length; i++) {
                            str = str + "<option value = '" + data1[i].id + "'>" + data1[i].name + "</optiom>";
                        }
                        $("#fl-reservoir").html(str);
                    }
                });
            }
        }

        $(function () {


            //AJAX 批量添加仓位
            $("#excel_upload_btn").click(function () {
                var excel = $("#excel").val();
                if (excel == '') {
                    alert("请选择文件");
                    return false;
                } else if (excel.lastIndexOf(".xls") < 0) {//可判断以.xls和.xlsx结尾的excel
                    alert("只能上传Excel文件");
                } else {
                    var url = "/ajax_stock/shelf_excel_import";
                    var formData = new FormData($('#execl_form')[0]);

                    // var formData = new FormData();
                    // formData.append("excel", document.getElementById("excel").files[0]);

                    $.ajax({
                        url: url,
                        type: "post",
                        data: formData,
                        success: function (result) {
                            // console.log(result);
                            if(result.code == 0){
                            alert(result.msg);
                            location.reload(true);
                            }else{
                                alert(result.msg)
                            }
                            // if (result.code == 0) {
                            //     alert("批量导入成功:新增" + result.data + "条仓位信息");
                            //     // window.location.href = "/shelf/shelf";
                            // } else {
                            //     alert("更新失败:" + result.msg());
                            // }


                            // alert(JSON.stringify(result));
                            return false;
                        },
                        complete: function(XMLHttpRequest){
                            if (XMLHttpRequest.getResponseHeader("REDIRECT")=="NO_ACCESS")alert("无权限访问");
                        },
                        error: function () {
                            alert("excel上传失败");
                        },
                        cache: false,
                        contentType: false,
                        processData: false
                    });
                }

                return false;
            })

            /*$("#fl-store_house").change(function () {
                alert('aaa');
                var id = $("#fl-store_house").val();
                $.ajax({
                    type:"POST",
                    url:"/shelf/reservoir",
                    data:id,
                    dataType: 'json',
                    contentType: 'application/json',
                    success:function(data){
                        var str = "";
                        for(var i in data) {
                            str = str+"<option value = '"+data[i].id+"'>"+data[i].name+"</optiom>";
                        }
                        $("#fl-reservoir").html(str);
                    }
                });
            });*/
            // 新增
            $('.add').click(function () {
                Tips.alert({
                    title: '新增仓位',
                    content: $('#tpl-1').html(),
                    before: function () {
                        var number = $('#fl-number');
                        // var name = $('#fl-name');
                        var store_house = $('#fl-store_house');
                        var reservoir = $('#fl-reservoir');
                        if (number.val() == '') {
                            alert("仓位编码不能为空");
                            return false;
                        }
                        // if (name.val() == '') {
                        //     alert("仓位名称不能为空");
                        //     return false;
                        // }
                        if (store_house.val() == '' || store_house.val() == '0' || store_house.val() == null) {
                            alert("所属仓库不能为空");
                            return false;
                        }
                        if (reservoir.val() == '' || reservoir.val() == '0' || reservoir.val() == null) {
                            alert("所属库区不能为空");
                            return false;
                        }

                        var dataObj = {
                            "name": number.val(),
                            "number": number.val(),
                            "houseId": store_house.val(),
                            "reservoirId": reservoir.val()
                        };

                        // ajax处理
                        $.ajax({
                            url: '/ajax_stock/add_shelf',
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: JSON.stringify(dataObj),
                            success: function (res) {
                                if (res.code == 0) {
                                    Tips.close();
                                    location.reload(true);
                                } else {
                                    alert(res.msg);
                                }
                            },
                            complete: function(XMLHttpRequest){
                                if (XMLHttpRequest.getResponseHeader("REDIRECT")=="NO_ACCESS")alert("无权限访问");
                            },
                        });
                        return false;
                    }
                });
            });

            // 修改
            $('.edit').click(function () {
                var data_id = $(this).attr('data-id');
                var data_number = $(this).attr('data-number');
                var data_name = $(this).attr('data-name');
                var data_store_house = $(this).attr('data-store_house');
                var data_reservoir = $(this).attr('data-reservoir');
                Tips.alert({
                    title: '修改库区',
                    content: $('#tpl-1').html(),
                    render: function () {
                        $('#fl-number').val(data_number);
                        $('#fl-name').val(data_name);
                        $('#fl-store_house').val(data_store_house);
                        $('#fl-reservoir').val(data_reservoir);
                        //默认库位选择
                        addVideo(data_store_house);
                        $("#fl-reservoir").val(data_reservoir);
                    },
                    before: function () {
                        var number = $('#fl-number');
                        var name = $('#fl-name');
                        var store_house = $('#fl-store_house');
                        var reservoir = $('#fl-reservoir');
                        if (number.val() == '') {
                            alert("仓位编码不能为空");
                            return false;
                        }
                        if (name.val() == '') {
                            alert("仓位名称不能为空");
                            return false;
                        }
                        if (store_house.val() == '' || store_house.val() == '0' || store_house.val() == null) {
                            alert("所属仓库不能为空");
                            return false;
                        }
                        if (reservoir.val() == '' || reservoir.val() == '0' || reservoir.val() == null) {
                            alert("所属库区不能为空");
                            return false;
                        }

                        var dataObj = {
                            "id": data_id,
                            "name": name.val(),
                            "number": number.val(),
                            "store_house": store_house.val(),
                            "reservoir": reservoir.val()
                        };

                        // ajax处理
                        $.ajax({
                            url: '/ajax_stock/update_shelf',
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: JSON.stringify(dataObj),
                            success: function (res) {
                                if (res.code == 0) {
                                    Tips.close();
                                    location.reload(true);
                                } else {
                                    alert(res.msg);
                                }
                            },
                            complete: function(XMLHttpRequest){
                                if (XMLHttpRequest.getResponseHeader("REDIRECT")=="NO_ACCESS")alert("无权限访问");
                            },
                        });
                        return false;
                    }
                });
            });

            // 删除
            $('.delete').click(function () {
                var id = $(this).attr('data-id');
                Tips.confirm({
                    title: '删除货架',
                    content: '您确定要删除该货架吗？',
                    define: function () {
                        // ajax处理
                        // .. id
                        // ajax处理
                        $.ajax({
                            url: '/ajax_stock/del_stock_house',
                            type: "POST",
                            dataType: 'json',
                            contentType: 'application/json',
                            data: id,
                            success: function (res) {
                                Tips.alert(res.msg, function () {
                                    location.reload(true);
                                });
                            },
                            complete: function(XMLHttpRequest){
                                if (XMLHttpRequest.getResponseHeader("REDIRECT")=="NO_ACCESS")alert("无权限访问");
                            },
                        });
                    }
                });
            });
        });

    </script>
    <!-- end sp-wrapper -->

</div>
</body>
<html>
